package com.huatai.common.httputil;

/**
 * @program: ht-system
 * @description:
 * @author: songxiude
 * @create: 2023-04-25 15:35
 **/

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import okhttp3.Cookie;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public interface ResponseSpec {
	int code();

	String message();

	default boolean isOk() {
		return false;
	}

	boolean isRedirect();

	Headers headers();

	default ResponseSpec headers(Consumer<Headers> consumer) {
		consumer.accept(this.headers());
		return this;
	}

	List<Cookie> cookies();

	default ResponseSpec cookies(Consumer<List<Cookie>> consumer) {
		consumer.accept(this.cookies());
		return this;
	}

	String asString();

	byte[] asBytes();

	InputStream asStream();

	JsonNode asJsonNode();

	@Nullable
	<T> T asValue(Class<T> valueType);

	@Nullable
	<T> T asValue(TypeReference<T> typeReference);

	<T> List<T> asList(Class<T> valueType);

	<K, V> Map<K, V> asMap(Class<?> keyClass, Class<?> valueType);

	<V> Map<String, V> asMap(Class<?> valueType);

	<T> T asDomValue(Class<T> valueType);

	<T> List<T> asDomList(Class<T> valueType);

	File toFile(File file);

	Path toFile(Path path);

	@Nullable
	MediaType contentType();

	long contentLength();

	Request rawRequest();

	@Nullable
	default ResponseSpec rawRequest(Consumer<Request> consumer) {
		consumer.accept(this.rawRequest());
		return this;
	}

	Response rawResponse();

	default ResponseSpec rawResponse(Consumer<Response> consumer) {
		consumer.accept(this.rawResponse());
		return this;
	}

	@Nullable
	ResponseBody rawBody();

	@Nullable
	default ResponseSpec rawBody(Consumer<ResponseBody> consumer) {
		consumer.accept(this.rawBody());
		return this;
	}
}

